package com.hsteach.web.controller.activity;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.hsteach.common.annotation.Anonymous;
import com.hsteach.common.config.RuoYiConfig;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.domain.R;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.common.utils.poi.ExcelUtil;
import com.hsteach.dao.domain.activity.Activities;
import com.hsteach.dao.domain.activity.ActivityEnroll;
import com.hsteach.framework.config.ServerConfig;
import com.hsteach.framework.feign.client.upload.vo.UploadRet;
import com.hsteach.framework.feign.client.xgk.dto.ret.GetClassListByIdsRet;
import com.hsteach.framework.web.service.UploadService;
import com.hsteach.service.activity.ActivitiesService;
import com.hsteach.service.activity.ActivityEnrollService;
import com.hsteach.web.controller.activity.dto.*;
import com.hsteach.web.controller.activity.vo.EnrollTree;
import com.hsteach.web.dto.PageDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 活动列表Controller
 * 
 * @author ruoyi
 * @date 2023-07-07
 */
@Api(value = "活动相关", tags = "活动相关")
@RestController
@RequestMapping("/system/activities")
public class ActivitiesController extends BaseController
{
    @Autowired
    private ActivitiesService activitiesService;


    @Autowired
    private ActivityEnrollService activityEnrollService;

    @Autowired
    private RuoYiConfig config;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private UploadService uploadService;

    /**
     * 查询活动列表
     */
    @ApiOperation("查询活动列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PageDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Activities query = new Activities();
        query.setOrgCode(SecurityUtils.getAuthInfo().getOrgCode());
        List<Activities> list = activitiesService.selectTbActivitiesList(query);
        return getDataTable(list);
    }

    /**
     * 查询我发布的活动列表
     */
    @ApiOperation("查询我发布的活动列表")
    @PostMapping("/listMine")
    public TableDataInfo listMine(@RequestBody PageDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Activities query = new Activities();
        query.setCreateBy(SecurityUtils.getAuthInfo().getId());
        query.setOrgCode(SecurityUtils.getAuthInfo().getOrgCode());
        List<Activities> list = activitiesService.selectTbActivitiesList(query);
        return getDataTable(list);
    }

    /**
     * 查询活动报名人员列表
     */
    @ApiOperation("查询活动报名人员列表")
    @GetMapping("/listEnroll")
    public R<List<EnrollTree>> listEnroll(@RequestParam(value = "id") Long id)
    {
        ActivityEnroll enroll = new ActivityEnroll();
        enroll.setActivityId(id);
        List<EnrollTree> enrollTrees = activityEnrollService.selectTbActivityEnrollTree(enroll);
        return R.ok(enrollTrees);
    }

    /**
     * 查询活动尚未报名人员列表
     */
    @ApiOperation("查询活动尚未报名人员列表")
    @GetMapping("/listNotEnroll")
    public R<List<EnrollTree>> listNotEnroll(@RequestParam(value = "id") Long id)
    {
        List<EnrollTree> enrollTrees = activityEnrollService.selectTbActivityNotEnrollTree(id);
        return R.ok(enrollTrees);
    }

    /**
     * 发送报名提醒
     */
    @ApiOperation("发送报名提醒")
    @PostMapping("/sendMsg")
    public AjaxResult sendMsgToNotEnroll(@RequestBody SendMsgDto dto)
    {
        activityEnrollService.sendMsgToNotEnroll(dto.getMsg(), dto.getId());
        return AjaxResult.success();
    }

    /**
     * 报名
     */
    @ApiOperation("活动报名")
    @PostMapping("/enroll")
    public AjaxResult enroll(@RequestBody EnrollDto dto)
    {
        activitiesService.enroll(dto.getId());
        return AjaxResult.success();
    }

    /**
     * 查询班级列表
     */
    @ApiOperation("查询可发布班级列表，新增活动时不传id，修改活动时传活动id")
    @GetMapping("/listClasses")
    public TableDataInfo listClasses(@RequestParam(value = "id", required = false) Long id)
    {
        List<GetClassListByIdsRet> list = activitiesService.listClasses(id);
        return getDataTable(list);
    }

    /**
     * 获取活动列表详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("查询活动详情")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(activitiesService.selectTbActivitiesById(id));
    }

    /**
     * 新增活动列表
     */
    @ApiOperation("新增活动")
    @PostMapping
    public AjaxResult add(@RequestBody AddActivityDto dto)
    {
        Long i = activitiesService.addActivity(dto);
        return toAjax(i);
    }

    /**
     * 修改活动列表
     */
    @ApiOperation("修改活动")
    @PutMapping
    public AjaxResult edit(@RequestBody UpdateActivityDto dto)
    {
        return toAjax(activitiesService.updateTbActivities(dto));
    }

    /**
     * 导出活动报名人员列表
     */
    @SneakyThrows
    @ApiOperation("导出活动报名人员列表")
    @PostMapping("/export")
    public AjaxResult export(HttpServletResponse response, @RequestBody ExportActivityDto dto)
    {
        ActivityEnroll query = new ActivityEnroll();
        query.setActivityId(dto.getActivityId());
        List<ActivityEnroll> list = activityEnrollService.selectTbActivityEnrollList(query);
        ExcelUtil<ActivityEnroll> util = new ExcelUtil<ActivityEnroll>(ActivityEnroll.class);
//        String fileName = UUID.fastUUID() + ".xlsx";
//        String path = config.getProfile() + "/" + fileName;
//        logger.info(path);
//        File file = new File(path);
//        if (!file.exists()) {
//            file.createNewFile();
//        }
//        OutputStream outputStream = new FileOutputStream(file);
        // todo MGB
        Workbook wb = util.exportWb(list, "活动报名人员数据");
        UploadRet upload = uploadService.upload(wb, "活动报名人员数据.xlsx");
        AjaxResult success = AjaxResult.success();
        Map<String, String> data = Maps.newHashMap();
        data.put("url", upload.getUrl());
        success.put("data", data);
        return success;
    }

    /**
     * 删除活动列表
     */
    @ApiOperation("删除活动")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(activitiesService.updateDeleteById(id));
    }

    /**
     * 区域驾驶舱小程序统计接口
     * @param id
     * @return
     */
    @ApiOperation("区域驾驶舱小程序统计接口")
    @GetMapping("/area/app")
    @Anonymous
    public AjaxResult statisticsAreaApp()
    {
        return AjaxResult.success(activitiesService.statisticsAreaApp());
    }
}

